package xin.clips.web.controller.monitor;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.clips.common.annotation.Log;
import xin.clips.common.enums.BusinessType;
import xin.clips.system.domain.SysVisitLog;
import xin.clips.system.service.ISysVisitLogService;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.utils.poi.ExcelUtil;
import xin.clips.common.core.page.TableDataInfo;

/**
 * 系统访问日志Controller
 *
 * @author yuanzy
 * @date 2023-02-27
 */
@Controller
@RequestMapping("/monitor/visistlog")
public class SysVisitLogController extends BaseController
{
    private String prefix = "monitor/visistlog";

    @Autowired
    private ISysVisitLogService sysVisitLogService;

    @RequiresPermissions("system:visistlog:view")
    @GetMapping()
    public String log()
    {
        return prefix + "/visistlog";
    }

    /**
     * 查询系统访问日志列表
     */
    @RequiresPermissions("monitor:visistlog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysVisitLog sysVisitLog)
    {
        startPage();
        List<SysVisitLog> list = sysVisitLogService.selectSysVisitLogList(sysVisitLog);
        return getDataTable(list);
    }

    /**
     * 导出系统访问日志列表
     */
    @RequiresPermissions("monitor:visistlog:export")
    @Log(title = "系统访问日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysVisitLog sysVisitLog)
    {
        List<SysVisitLog> list = sysVisitLogService.selectSysVisitLogList(sysVisitLog);
        ExcelUtil<SysVisitLog> util = new ExcelUtil<SysVisitLog>(SysVisitLog.class);
        return util.exportExcel(list, "系统访问日志数据");
    }

    /**
     * 新增系统访问日志
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存系统访问日志
     */
    @RequiresPermissions("monitor:visistlog:add")
    @Log(title = "系统访问日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysVisitLog sysVisitLog)
    {
        return toAjax(sysVisitLogService.insertSysVisitLog(sysVisitLog));
    }

    /**
     * 修改系统访问日志
     */
    @RequiresPermissions("monitor:visistlog:edit")
    @GetMapping("/edit/{visitId}")
    public String edit(@PathVariable("visitId") Long visitId, ModelMap mmap)
    {
        SysVisitLog sysVisitLog = sysVisitLogService.selectSysVisitLogByVisitId(visitId);
        mmap.put("sysVisitLog", sysVisitLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统访问日志
     */
    @RequiresPermissions("monitor:visistlog:edit")
    @Log(title = "系统访问日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysVisitLog sysVisitLog)
    {
        return toAjax(sysVisitLogService.updateSysVisitLog(sysVisitLog));
    }

    /**
     * 删除系统访问日志
     */
    @RequiresPermissions("monitor:visistlog:remove")
    @Log(title = "系统访问日志", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysVisitLogService.deleteSysVisitLogByVisitIds(ids));
    }


    @Log(title = "系统访问日志", businessType = BusinessType.CLEAN)
    @RequiresPermissions("monitor:visistlog:remove")
    @PostMapping("/clean")
    @ResponseBody
    public AjaxResult clean()
    {
        sysVisitLogService.cleanVisitLog();
        return success();
    }
}
